#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <strings.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
    if (argc < 3)
    {
        std::cerr<<"Usage: "<<argv[0]<<" server_op server_port"<<std::endl;
        return 1;
    }
    int socketfd = socket(AF_INET, SOCK_DGRAM, 0);
    std::string ip = argv[1];
    std::string port = argv[2];
    struct sockaddr_in peer;
    peer.sin_family = AF_INET;
    peer.sin_port = htons(std::stoi(port));
    peer.sin_addr.s_addr = inet_addr(ip.c_str());
    while (true)
    {
        std::string message;
        std::getline(std::cin, message);
        sendto(socketfd, message.c_str(), sizeof(message), 0, (const sockaddr *)&peer, sizeof(peer));
        sockaddr_in sd;
        char buffer[1024];
        socklen_t len = sizeof(sd);
        int n = recvfrom(socketfd,buffer,sizeof(buffer)-1,0,(sockaddr *)&sd, &len);
        buffer[n]='\0';
        std::cout<<buffer<<std::endl;
    }
    return 0;
}